package com.dpteam.service.impl;

import com.dpteam.bean.ProductInfo;
import com.dpteam.dao.CategoryDao;
import com.dpteam.dao.ProductDao;
import com.dpteam.dao.ProviderDao;
import com.dpteam.model.Category;
import com.dpteam.model.Product;
import com.dpteam.model.Provider;
import com.dpteam.service.ProductService;
import com.dpteam.util.Page;
import com.dpteam.util.TransformUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by xwh on 2016/1/13.
 */
@Service
@Transactional
public class ProductServiceImpl implements ProductService {

    @Resource
    private ProductDao productDao;

    @Resource
    private CategoryDao categoryDao;

    @Resource
    private ProviderDao providerDao;

    private static Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);

    @Override
    public boolean addProduct(String barcode, String productName, int categoryId, float buyPrice, float setPrice, int providerId, String description) {
        logger.info("添加商品[barcode:{},productName:{},categoryId:{},buyPrice:{},setPrice:{},providerId:{},description:{}]", barcode, productName, categoryId, buyPrice, setPrice, providerId, description);
        if (productDao.findByBarcode(barcode) != null) {
            return false;
        }
        Category category = categoryDao.get(Category.class, categoryId);
        if (category == null) {
            return false;
        }
        Provider provider = providerDao.get(Provider.class, providerId);
        if (provider == null) {
            return false;
        }
        Product product = new Product();
        product.setBarcode(barcode);
        product.setProductName(productName);
        product.setCategory(category);
        product.setBuyPrice(buyPrice);
        product.setSetPrice(setPrice);
        product.setStorage(0);
        product.setProvider(provider);
        product.setDescription(description);
        productDao.save(product);
        return true;
    }

    @Override
    public boolean updateProduct(int id, float setPrice) {
        logger.info("修改商品[id:{}, setPrice:{}]", id, setPrice);
        Product product = productDao.get(Product.class, id);
        if (product == null) {
            return false;
        }
        product.setSetPrice(setPrice);
        productDao.saveOrUpdate(product);
        return true;
    }

    @Override
    public List<ProductInfo> findAllProductPage(Page page) {
        logger.info("findAllProductPage");
        List<Product> products = productDao.findAllProductPage(page);
        List<ProductInfo> productInfos = new ArrayList<>();
        for (Product product : products) {
            productInfos.add(TransformUtil.fromProduct(product));
        }
        return productInfos;
    }

    @Override
    public ProductInfo findProductByBarcode(String barcode) {
        logger.info("findProductByBarcode");
        Product product = productDao.findByBarcode(barcode);
        if (product == null) {
            return null;
        } else {
            return TransformUtil.fromProduct(product);
        }
    }

    @Override
    public List<Category> findAllCategory() {
        logger.info("findAllProductCategory");
        return categoryDao.findAll();
    }

    @Override
    public List<Provider> findAllProvider() {
        logger.info("findAllProvider");
        return providerDao.findAllProvider();
    }

    @Override
    public List<ProductInfo> find(String name) {
        logger.info("通过商品名查询商品 [name:{}]", name);
        List<Product> products = productDao.findByProductName(name);
        List<ProductInfo> productInfos = new ArrayList<>();
        for (Product product: products) {
            productInfos.add(TransformUtil.fromProduct(product));
        }
        return productInfos;
    }

    @Override
    public List<ProductInfo> find(String name, Page page) {
        logger.info("通过商品名查询商品 [name:{}]", name);
        List<Product> products = productDao.findLikeProductName(name, page);
        List<ProductInfo> productInfos = new ArrayList<>();
        for (Product product: products) {
            productInfos.add(TransformUtil.fromProduct(product));
        }
        return productInfos;
    }
}
